package com.pkt.item.controller;

import com.item.piketao.bo.SpuBo;
import com.item.piketao.dao.Sku;
import com.item.piketao.dao.SpuDetail;
import com.piketao.common.pojo.PageInfo;
import com.pkt.item.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: xinglongge
 * @Date: 2020/3/4 19 08
 * @Description:
 */
@Controller
@RequestMapping
public class GoodsController {
    private GoodsService goodsService;

    @Autowired
    public GoodsController(GoodsService goodsService) {
        this.goodsService = goodsService;
    }

    /**
     * 根据条件分页查询spu
     * @param key
     * @param saleable
     * @param page
     * @param rows
     * @return
     */
    @GetMapping("spu/page")
    public ResponseEntity<PageInfo<SpuBo>> querySpuByPage(
            @RequestParam(value = "key" ,required = false)String key,
            @RequestParam(value = "saleable" ,required = false)Boolean saleable,
            @RequestParam(value = "page" ,required = false,defaultValue = "1")Integer page,
            @RequestParam(value = "rows" ,required = false,defaultValue = "6")Integer rows
    ){

        PageInfo<SpuBo> spuBoPageInfo =  this.goodsService.querySpuByPage(key,saleable,page,rows);
        if (spuBoPageInfo==null|| CollectionUtils.isEmpty(spuBoPageInfo.getItems())){
            return ResponseEntity.noContent().build();
        }
        return ResponseEntity.ok(spuBoPageInfo);
    }

    @PostMapping("goods")
    public ResponseEntity<Void> saveGoods(@RequestBody SpuBo spuBo){
        goodsService.saveGoods(spuBo);
        return ResponseEntity.ok().build();
    }

    /**
     * 根据spuId查询spuDetail
     * @param id
     * @return
     */
    @GetMapping("/spu/detail/{spuId}")
    public ResponseEntity<SpuDetail> querySpuDetailById(@PathVariable("spuId") Long id) {
        SpuDetail detail = this.goodsService.querySpuDetailById(id);
        if (detail == null) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return ResponseEntity.ok(detail);
    }

    /**
     *通过spuId查询skus
     * @param id
     * @return
     */
    @GetMapping("sku/list")
    public ResponseEntity<List<Sku>> querySkuBySpuId(@RequestParam("id") Long id) {
        List<Sku> skus = this.goodsService.querySkuBySpuId(id);
        if (skus == null || skus.size() == 0) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return ResponseEntity.ok(skus);
    }

    /**
     * 更新商品信息
     * @param spu
     * @return
     */
    @PutMapping("goods")
    public ResponseEntity<Void> updateGoods(@RequestBody SpuBo spu) {
        try {
            this.goodsService.update(spu);
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
}
